home *** CD-ROM | disk | FTP | other *** search
- /*
- * FFT.C -- Show FFT analyzer spectra
- *
- * Copyright (C) 1991 by Alef Null. All rights reserved.
- * Author(s): Jarkko Vuori, OH2LNS
- * Modification(s):
- */
-
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <conio.h>
- #include <ctype.h>
- #include <math.h>
- #include "dspldrv.h"
- #include "serial.h"
-
-
- #define DX 512 // number of horisontal lines
- #define DY 256 // vertical lines
-
- #define PORT 1 // default serial port number
- #define BAUD 19200 // baud rate
-
- #define POINTS 1024 // number of points in FFT
-
-
- /* local program status */
- static struct {
- unsigned ctrlC:1;
- } flags;
-
- /* DSP CARD 3 FFT analyzer status */
- static struct {
- long version,
- samplef,
- N;
- } card_status;
-
-
- /*
- * CTRL-C handler
- */
- void cdecl CtrlCHandler(void) {
- flags.ctrlC = 1;
- }
-
-
- /*
- * handle one word from DSP CARD 3
- */
- void process(int wordno, long d) {
- static double maxdB, prev_maxdB, xmaxdB, prev_xmaxdB;
- double dB;
- int bin;
-
- switch (wordno) {
- case 1:
- card_status.version = d;
- break;
-
- case 2:
- card_status.samplef = d;
- break;
-
- case 3:
- card_status.N = d;
- break;
-
- default:
- bin = wordno-4;
-
- /* one whole word received, first calculate dB */
- dB = d > 0 ? 10.0*log10((double)d) : 0.0;
-
- /* then check if it was maximum */
- if (dB > maxdB && bin > 3) {
- maxdB = dB;
- xmaxdB = (double)bin;
- }
- if (dB > prev_maxdB && bin > 3 && abs(bin-(int)xmaxdB) > 10) {
- prev_maxdB = dB;
- prev_xmaxdB = (double)bin;
- }
-
- /* plot it */
- PlotPoint(bin, (int)(3.0*dB+.5));
- break;
-
- case POINTS/2+3:
- /* last word received */
- PlotStatus(2*card_status.N, card_status.samplef);
- PlotMarker((int)xmaxdB, (int)(3.0*maxdB+.5), xmaxdB*(double)card_status.samplef/(double)POINTS, maxdB,
- (int)prev_xmaxdB, (int)(3.0*prev_maxdB+.5), prev_xmaxdB*(double)card_status.samplef/(double)POINTS, prev_maxdB);
- prev_maxdB = maxdB = 0.0;
- break;
- }
- }
-
-
- int cdecl main(int argc, char *argv[]) {
- long d;
- int c, state = 0, port = PORT;
- char option;
-
- /* parse arguments */
- argc--; argv++;
- while(argc > 0) {
- switch(**argv) {
- case '-':
- switch(option = *(++(*argv))) {
- /* port selection */
- case 'p':
- case 'P':
- port = atoi(++(*argv));
- break;
-
- default:
- fprintf(stderr, "unknown option '%c', type 'fft ?' to get the allowed options\n", option);
- break;
- }
- break;
-
- case '?':
- fprintf(stderr, "usage: fft [-p<portno>]\n");
- fprintf(stderr, "\t-p<portno> uses the specified (1 (default) or 2) serial port\n");
- return (0);
- break;
-
- default:
- fprintf(stderr, "option must be preceded by '-'\n");
- break;
- }
-
- argc--; argv++;
- }
-
- signal(SIGINT, CtrlCHandler);
- if (!OpenSerial(port, BAUD)) {
- fprintf(stderr, "can't open file serial port COM%d\n", port);
- return (1);
- }
-
- if (InitDspl(DX, DY)) {
- fprintf(stderr, "%s: no support for needed graphics device\n", __FILE__);
- CloseSerial();
- return (1);
- }
-
- while (!flags.ctrlC) {
- /* check if user want something */
- if (kbhit()) {
- c = getch();
-
- if (c == '.')
- break;
- else if (c == '+' || c == '-')
- WriteSerial(c);
- }
-
- /* then check if DSP CARD 3 wants something */
- if ((c = ReadSerial()) != -1)
- switch (state) {
- case 0: // wait for header
- if (c == 0x64)
- state++;
- break;
-
- case 1:
- if (c == 0x09)
- state++;
- else
- state = 0;
- break;
-
- case 2:
- if (c == 0x01)
- state++;
- else
- state = 0;
- break;
-
- default: // read data
- /* collect three bytes (24 bits) */
- switch (state % 3) {
- case 0:
- d = (long)c;
- break;
-
- case 1:
- d |= (long)c << 8;
- break;
-
- case 2:
- d |= (long)c << 16;
-
- process(state/3, d);
- break;
- }
-
- /* and finally, check if it was the last line of spectra */
- if (++state >= 3*(POINTS/2+1+3))
- state = 0;
- break;
- }
- }
-
- CloseSerial();
- ReleaseDspl();
- return (0);
- }
-